package com.atguigu.gmall.seckill.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.seckill.biz.SeckillBizService;
import com.atguigu.gmall.seckill.vo.SeckillOrderSubmitVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/activity/seckill/auth")
@RestController
public class SeckillApiController {

    @Autowired
    SeckillBizService seckillBizService;

    @ApiOperation("生成秒杀码")
    @GetMapping("/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillCode (@PathVariable("skuId") Long skuId){
        String code = seckillBizService.generateSeckillCode(skuId);
        return Result.ok(code);
    }

    @ApiOperation("下秒杀单：秒杀开始排队")
    @PostMapping("/seckillOrder/{skuId}")
    public Result seckillOrder (@PathVariable("skuId") Long skuId,
                                @RequestParam("skuIdStr") String code){
        //秒杀下单
        seckillBizService.seckillOrder(skuId,code);
        return Result.ok();
    }

    @ApiOperation("检查秒杀单状态")
    @RequestMapping("/checkOrder/{skuId}")
    public Result checkOrder(@PathVariable("skuId") Long skuId){
        //检查秒杀单状态
        ResultCodeEnum resultCodeEnum = seckillBizService.checkOrder(skuId);
        return Result.build("", resultCodeEnum);
    }

    @ApiOperation("提交秒杀单")
    @RequestMapping("/submitOrder")
    public Result submitOrder(@RequestBody SeckillOrderSubmitVo submitVo){
        Long orderId = seckillBizService.submitOrder(submitVo);
        return Result.ok(orderId.toString());
    }
}
